package com.bianxianmao;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bianxianmao.base.BaseFragment;
import com.bianxianmao.duobao.fragment.DuobaoFragment;
import com.bianxianmao.find.activity.AllreturnActivity;
import com.bianxianmao.find.fragment.FindFragment;
import com.bianxianmao.lastknow.fragment.LastkowFragment;
import com.bianxianmao.myinfo.activity.LoginActivity;
import com.bianxianmao.myinfo.fragment.MyinfoFragment;

import java.util.ArrayList;
import java.util.List;

import static com.igexin.sdk.GTServiceManager.context;

public class MainActivity extends FragmentActivity {
    private List<BaseFragment> fragments;
    private int position;
    private BaseFragment mContext;
    private RadioGroup rgmain;
    private RadioButton duobao;
    private RadioButton lastknow;
    private RadioButton find;
    private RadioButton myinfo;
    private PopupWindow popupWindow;
    private Dialog dialog;
    private ImageView close;
    private Button redpocket;
    private SharedPreferences preferences;
    private Dialog mydialog;
    private Button leavefor;
//    private Boolean login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //给弹窗设置首选项存储
        preferences = getSharedPreferences("count", MODE_WORLD_READABLE);
        int count = preferences.getInt("count", 0);
        if (count == 0) {
            initDialog();
        }
        preferences.edit().putInt("count", ++count).commit();
//        initDialog2();
        rgmain = (RadioGroup) findViewById(R.id.rg_main);
        duobao = (RadioButton) findViewById(R.id.rb_duobao);
        lastknow = (RadioButton) findViewById(R.id.rb_lastknow);
        find = (RadioButton) findViewById(R.id.rb_find);
        myinfo = (RadioButton) findViewById(R.id.rb_myinfo);
        ImageView ivgif = (ImageView) findViewById(R.id.iv_gif);
        ivgif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AllreturnActivity.class));
            }
        });

        initFragment();
//        if (!login){
//           mydialog.dismiss();
//        }else {
//            mydialog.show();
//        }
        initListener();
    }

    //弹出不中全返对话框
    private void initDialog2() {
        mydialog = new Dialog(context, R.style.edit_AlertDialog_style);
        mydialog.setContentView(R.layout.allreturn_dialog);
        leavefor = (Button) mydialog.findViewById(R.id.btn_leavefor);
        close = (ImageView) mydialog.findViewById(R.id.iv_close);
        mydialog.show();
        mydialog.setCanceledOnTouchOutside(false);
//        Window w = dialog.getWindow();
//        WindowManager.LayoutParams lp = w.getAttributes();
//        lp.x = 0;
//        lp.y = 40;
//        dialog.onWindowAttributesChanged(lp);
        //设置关闭监听
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mydialog.cancel();
            }
        });
        //点击跳转到登录页面
        leavefor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, AllreturnActivity.class));
                mydialog.hide();
            }
        });
    }

    private void initDialog() {
        final Context context = MainActivity.this;
        dialog = new Dialog(context, R.style.edit_AlertDialog_style);
        dialog.setContentView(R.layout.red_pocket_dialog);
        redpocket = (Button) dialog.findViewById(R.id.btn_redbag);
        close = (ImageView) dialog.findViewById(R.id.iv_close);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
//        Window w = dialog.getWindow();
//        WindowManager.LayoutParams lp = w.getAttributes();
//        lp.x = 0;
//        lp.y = 40;
//        dialog.onWindowAttributesChanged(lp);
        //设置关闭监听
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        //点击跳转到登录页面
        redpocket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, LoginActivity.class));
                dialog.dismiss();
            }
        });
    }

    //初始化监听
    private void initListener() {
        rgmain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb_duobao://全民夺宝
                        position = 0;
                        break;
                    case R.id.rb_lastknow://最新知晓
                        position = 1;
                        break;
                    case R.id.rb_find://发现
                        position = 2;
                        break;
                    case R.id.rb_myinfo://我的
                        position = 3;
                        break;
                }
                BaseFragment baseFragment = getFragment(position);
                switchFragment(mContext, baseFragment);
            }

        });
        rgmain.check(R.id.rb_duobao);
    }

    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (mContext != nextFragment) {
            mContext = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.framelayout, nextFragment).commit();
                } else {
                    //隐藏当前Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }

    }

    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);
            return baseFragment;
        }
        return null;
    }

    //初始化fragment
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new DuobaoFragment());
        fragments.add(new LastkowFragment());
        fragments.add(new FindFragment());
        fragments.add(new MyinfoFragment());
    }

    @Override
    public void onBackPressed() {
        dialog.dismiss();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dialog.dismiss();
    }

}
